package qc.module.ehs.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.EhsWarehouseInventoryOperateTypeEnum;
import qc.common.core.utils.DateUtil;
import qc.module.ehs.dto.warehouseinventory.WareHouseInventoryAddDto;
import qc.module.ehs.dto.warehouseinventory.WareHouseInventoryDto;
import qc.module.ehs.entity.EhsWarehouseInventory;

import java.util.Date;
import java.util.List;

/**
 * EhsWarehouseInventoryMapper
 *
 * @author QuCheng Tech
 * @since 2024/11/11
 */
@Mapper
public interface EhsWarehouseInventoryMapper {
    EhsWarehouseInventoryMapper MAPPER = Mappers.getMapper(EhsWarehouseInventoryMapper.class);

    @Mapping(target = "tm", source = "tm", dateFormat = "yyyy-MM-dd")
    @Mapping(target = "optm", source = "optm", dateFormat = "yyyy-MM-dd")
    @Mapping(target = "checktm", source = "checktm", dateFormat = "yyyy-MM-dd")
    @Mapping(target = "optype", source = "optype.index")
    WareHouseInventoryDto toDto(EhsWarehouseInventory en);

    List<WareHouseInventoryDto> toDtoList(List<EhsWarehouseInventory> ens);

    @Mapping(target = "tm", ignore = true)//出入库时间单独进行转换，精确到日
    @Mapping(target = "optm", ignore = true)//出入库的记录写入时间默认为当前时间
    @Mapping(target = "optype", ignore = true)
    EhsWarehouseInventory toEntity(WareHouseInventoryAddDto addDto);

    default EhsWarehouseInventory addDtoToEntity(WareHouseInventoryAddDto dto) {
        EhsWarehouseInventory en = EhsWarehouseInventoryMapper.MAPPER.toEntity(dto);
        //时间转换，精确到日
        Date date = DateUtil.parseDate(dto.getTm());
        en.setTm(DateUtil.getDayDate(date));
        //设置出入库记录写入时间为当前时间
        en.setOptm(DateUtil.getNowDate());

        en.setOptype(getEhsWarehouseInventoryOperateTypeEnum(dto.getOptype()));

        return en;
    }

    static EhsWarehouseInventoryOperateTypeEnum getEhsWarehouseInventoryOperateTypeEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (EhsWarehouseInventoryOperateTypeEnum item : EhsWarehouseInventoryOperateTypeEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }

        //默认为1-出库
        return EhsWarehouseInventoryOperateTypeEnum.OUTBOUND;
    }
}
